package itsm.isperp.module.controller.sparepartOld;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.sparepart.SptSparepartOut;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectMemberService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.sparepart.SptSparepartOutService;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件管理控制器
 * 
 * @author lizx
 * @date 2013-9-12
 */
@Controller
@RequestMapping("sparepart/sparepartOut/old")
public class SptSparepartOutOldController extends
		BaseProcessController<SptSparepartOut> {

	@Autowired
	protected SptSparepartOutService sptSparepartOutService;
 
	@Autowired
	ProjProjectMemberService projProjectMemberService;
	
	@Autowired
	protected ProjProjectService projProjectService;

	@Override
	public SptSparepartOutService getService() {
		return sptSparepartOutService;
	}

	@Override
	public String getPageTitle() {
		return "备件管理";
	}

	@Override
	public String getProceessKey() {
		return "sparepartOutProcess";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		SptSparepartOut model = null;

		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");

		if (StringUtils.isEmpty(id)) {
			model = new SptSparepartOut();
			model.setId(HibernateUUIDGenerator.generate());
			String username=ContextHolder.getLoginUsername();
			model.setCreator(username);
			model.setUseManId(username);
			model.setUseDate(new Date());

		} else {
			model = this.getService().findOne(id);

		}
		mv.addObject("useManName",
				appUserService.findByUsername(model.getUseManId())
						.getFullname());

		mv.addObject("model", model);
		mv.addObject("projects",
				projProjectService.findAllPublished("id", "name"));

		mv.addObject("creatorName",
				appUserService.findByUsername(model.getCreator()).getFullname());

		return mv;

	}
	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);
		
		
		//项目
		DataRequest dataProject=new JqGridRequest();
		dataProject.isPageable(false);
		dataProject.setSelectColumns("id","name");
		mv.addObject("projects", projProjectService.findAll(dataProject).getContent());
		
		return mv;
	}

}
